<template>
    <div>

        <div id="control">
            <div class="content">
                <div style="display: block;">
                    <p>账号</p>
                    <input type="text" placeholder="请输入账号" v-model="user.userName" />
                    <p>密码</p>
                    <input type="password" placeholder="请输入密码" v-model="user.password" />
                    <br />
                    <input type="submit" value="登录" @click.prevent="btn_login">
                </div>
            </div>
        </div>
    </div>
</template>
  
<script>
import axios from 'axios';
export default {
    // el: "#control",
    name: 'Login',
    data() {
        return {
            user: {
                userName: '',
                password: ''
            }
        }
    },
    methods: {
        btn_login() {
            if (this.user.username != '' && this.user.password != '') {
                //根据账号和密码做登录验证
                axios({
                    method: "post",
                    url: "http://localhost:8085/user/login",
                    data: this.user
                }).then((res) => {
                    // res:登录成功返回用户对象，登录失败需要给出对应的提示
                    console.log(res);
                    if (res.data.code == 200) {
                        //将用户数据存到localStorage中
                        // JSON.stringify:将json对象转换为字符串

                        localStorage.setItem("token", res.data.data.token);
                        //返回到登录之前的页面
                        alert('登录成功！！！')
                        this.$router.push('/home')
                    } else {
                        alert(res.data.message)
                    }
                })
            } else {
                alert("有未输入的信息！！")
            }
        }
    },

}
</script>
  
<style scoped>
#control {
    width: 340px;
    background: white;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
}

.item {
    width: 340px;
    height: 60px;
    background: #eeeeee;
}

.item div {
    width: 170px;
    height: 60px;
    display: inline-block;
    color: black;
    font-size: 18px;
    text-align: center;
    line-height: 60px;
    cursor: pointer;
}

.content {
    width: 100%;
}

.content div {
    margin: 20px 30px;
    display: none;
    text-align: left;
}

p {
    color: #4a4a4a;
    margin-top: 30px;
    margin-bottom: 6px;
    font-size: 15px;
}

.content input[type="text"],
.content input[type="password"] {
    width: 100%;
    height: 40px;
    border-radius: 3px;
    border: 1px solid #adadad;
    padding: 0 10px;
    box-sizing: border-box;
}

.content input[type="submit"] {
    margin-top: 40px;
    width: 100%;
    height: 40px;
    border-radius: 5px;
    color: white;
    border: 1px solid #adadad;
    background: #00dd60;
    cursor: pointer;
    letter-spacing: 4px;
    margin-bottom: 40px;
}

.active {
    background: white;
}

.item div:hover {
    background: #f6f6f6;
}
</style>